import axios from "axios";
import Nprogress from "nprogress";
import "nprogress/nprogress.css";

//封装aixos
const instance = axios.create({
  //baseURL 根目录
  baseURL: "http://localhost:1337",
  timeout: 5000,
});

//请求拦截
// Add a request interceptor
instance.interceptors.request.use(
  function (config) {
    //进度条开始
    Nprogress.start();
    // Do something before request is sent
    return config;
  },
  function (error) {
    // Do something with request error
    return Promise.reject(error);
  }
);
//响应拦截
instance.interceptors.response.use(
  function (response) {
    //进度条结束
    Nprogress.done();
    return response;
  },
  function (error) {
    //进度条结束
    Nprogress.done();
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  }
);

export const get = (url, params) => instance.get(url, { params });
export const post = (url, data) => instance.post(url, data);
